package com.djinfo.work.controller;

import com.djinfo.work.common.bean.AjaxJson;
import com.djinfo.work.entity.QsDicEntity;
import com.djinfo.work.entity.QsQuestionEntity;
import com.djinfo.work.service.QsDicService;
import com.djinfo.work.service.QsQuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;

/**
 * Created by gz on 2017/6/12.
 */
@Controller
@RequestMapping("/qsquestion")
public class QsQuestionController {
    @Autowired
    private QsQuestionService questionService;

    @ResponseBody
    @RequestMapping("/save.do")
    public AjaxJson save(QsQuestionEntity qs, HttpServletRequest request){
        String id = String.valueOf(request.getSession().getAttribute("userId"));
        AjaxJson ajaxJson = new AjaxJson();
        try{
            questionService.saveOrUpdate(qs,id);
            ajaxJson.setCode(101);
            ajaxJson.setMsg("success");
        }catch (Exception e){
            ajaxJson.setCode(201);
            ajaxJson.setMsg("error");
            e.printStackTrace();
        }
        return ajaxJson;
    }

    /**
     * 列表
     */
    @RequestMapping("/list.do")
    @ResponseBody
    public AjaxJson list(QsQuestionEntity qs, Integer pagenum, Integer pagesize){
        HashMap<String, Object> map = new HashMap<>();
        AjaxJson ajaxJson = new AjaxJson();
        try{
            List<QsQuestionEntity> res = questionService.page(qs,pagenum,pagesize);
            Integer count = questionService.getAllRowCount(qs);
            map.put("dataList",res);
            map.put("total",count);
            ajaxJson.setCode(101);
            ajaxJson.setMsg("success");
            ajaxJson.setResult(map);
        }catch (Exception e){
            ajaxJson.setCode(201);
            ajaxJson.setMsg("error");
        }
        return ajaxJson;
    }

    /**
     * 获取对象信息
     */
    @RequestMapping("/info.do")
    public ModelAndView info(QsQuestionEntity qs){
        HashMap<String, Object> map = new HashMap<>();
        AjaxJson ajaxJson = new AjaxJson();
        try{
            QsQuestionEntity res = questionService.selectOne(qs);
            map.put("data",res);
            ajaxJson.setCode(101);
            ajaxJson.setMsg("success");
            ajaxJson.setResult(map);
        }catch (Exception e){
            ajaxJson.setCode(201);
            ajaxJson.setMsg("error");
        }
        return new ModelAndView("qs_question/edit.html","map",map);
    }

    /**
     * 删除
     */
    @RequestMapping("/delete.do")
    @ResponseBody
    public AjaxJson delete(QsQuestionEntity qs){
        AjaxJson ajaxJson = new AjaxJson();
        try{
            questionService.delete(qs);
            ajaxJson.setCode(101);
            ajaxJson.setMsg("success");
        }catch (Exception e){
            ajaxJson.setCode(201);
            ajaxJson.setMsg("error");
        }
        return ajaxJson;
    }
}
